<template>
	<view class="new-users copy-data">
		<view class="bg"></view>
		<view class="head">
			<view class="user-card">
				<view class="bg"></view>
				<view class="user-info" style="margin-top: 170rpx;">
					<view class="headwear">
						<image class="avatar" :src="userData.headImgUrl?userData.headImgUrl:'../../static/img/f.png'"
							mode="" @click="goEdit()"></image>
					</view>
					<view class="info">
						<view class="name" v-if="userData.id">
							{{userData.nickname}}
							<image v-if="userData.level==0" class="svip" src="../../static/img/vip.png"></image>
							<image v-else class="vip" src="../../static/img/svip.png"></image>
						</view>
						<view class="name" v-else @click="login()">
							登陆/注册
						</view>
						<view class="num" @click="goEdit()">
							<view class="num-txt">会员号：{{userData.userName ? userData.userName : "xdl_0001"}}</view>
							<!-- <view class="icon">
									<image src="../../static/img/copy.png" mode=""></image>
								</view> -->
						</view>
					</view>
				</view>
				<view class="b-tag mt30">
					<view class="tag-item" v-for="(item, index) in likes" :key="index">
						<u-tag :text="item.navName" size="mini" bgColor="#454545" borderColor="#454545">
						</u-tag>
					</view>
				</view>

			</view>
			<div>
				<view class="message">
					<view class="G-font g-setting" @click="goEdit()"></view>
				</view>
			</div>
		</view>
		<view class="main">
			<view class="order-wrapper">
				<view class="order-hd b-flex">
					<view class="left">订单中心</view>
					<navigator class="right b-flex" hover-class="none" url="/pages/my/components/orderList"
						open-type="navigate">
						查看全部
						<text class="iconfont icon-xiangyou"></text>
					</navigator>
				</view>
				<view class="order-bd">
					<block v-for="(item,index) in orderMenu" :key="index">
						<navigator class="order-item" hover-class="none" :url="item.url">
							<view class="pic">
								<image :src="item.img" mode=""></image>
								<text class="order-status-num" v-if="item.num > 0">{{ item.num }}</text>
							</view>
							<view class="txt">{{item.title}}</view>
						</navigator>
					</block>
				</view>
			</view>
			<!-- 会员菜单 -->
			<view style="margin-top: 20rpx; border-radius: 16rpx; overflow: hidden;">
				<view class="title-box">我的服务</view>
				<view class="user-menus">

					<template v-for="(item, index) in MyMenus">
						<view v-if="item.url !== '#' && item.url !== 'service'" class="item" :key="index"
							@click="goMenuPage(item.url)">
							<image :src="item.pic"></image>
							<text>{{ item.name }}</text>
						</view>
						<view v-if="item.url == 'service'" class="service" :key="index">
							<button open-type="contact">
								<view>
									<image :src="item.pic"></image>
									<text>{{ item.name }}</text>
								</view>
							</button>
						</view>
					</template>
				</view>
			</view>
			<view class="xdl-ad mt20" v-if="partnerList.length>0">
				<u-swiper :list="partnerList" keyName="partnerBanner" indicator :height="150" indicatorMode="line"
					circular></u-swiper>
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<!-- <view class="ad-view">
				<ad adpid="1111111111" unit-id="adunit-d4546aeea070d8a5" type="feed" @load="adload" @error="aderror" />
			</view> -->
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import cache from '@/common/cache.js'
	export default {
		data() {
			return {
				likes: [],
				tabs: [],
				userData: {
					"headImgUrl": "../../static/img/f.png",
					"nickname": "新动力"
				},
				title: 'hello',
				partnerList: [],
				orderMenu: [{
						img: 'https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/order-all.png',
						title: '全部订单',
						url: '/pages/my/components/orderList?idx=0'
					},
					{
						img: 'https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/d-pay.png',
						title: '待支付',
						url: '/pages/my/components/orderList?idx=1'
					},
					{
						img: 'https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/d-user.png',
						title: '待使用',
						url: '/pages/my/components/orderList?idx=2'
					},
					{
						img: 'https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/used.png',
						title: '已使用',
						url: '/pages/my/components/orderList?idx=3'
					},
					{
						img: 'https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/tkuan.png',
						title: '退款/售后',
						url: '/pages/my/components/orderList?idx=4'
					}
				],
				MyMenus: [{
						"id": 251,
						"name": "我的卡券",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/card.png",
						"url": "/pages/my/components/card"
					},
					{
						"id": 249,
						"name": "我的收藏",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/save.png",
						"url": "/pages/my/components/save"
					},
					{
						"id": 251,
						"name": "邀请有礼",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/invite.png",
						"url": "/pages/my/components/invite"
					},
					{
						"id": 249,
						"name": "专属客服",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/service.png",
						"url": "service"
					},
					{
						"id": 219,
						"name": "商务合作",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/hzuo-icon.png",
						"url": "/pages/my/components/consociation"
					},
					{
						"id": 136,
						"name": "关于我们",
						"pic": "https://beluga-s3.oss-cn-hangzhou.aliyuncs.com/xdl-bucket/about.png",
						"url": "/pages/my/components/about"
					}

				]

			}
		},
		onLoad() {
			this.isLogin();
			this.loadPartner();
		},
		onShow() {
			this.$Request.get(this.$api.user.getUserInfo).then(res => {
				this.userData = res.data;
				cache.put('setUserData', res.data, 24 * 60 * 60 * 90);
				this.loadTabs();
			})
		},
		methods: {
			loadPartner() {
				this.$Request.get(this.$api.partner.getPartner).then(res => {
					this.partnerList = res.data;
				})
			},
			isLogin() {
				const value = cache.get('setUserData', false);
				console.log('login-value',value)
				if (!value) {
					this.login();
				} else {
					this.$Request.get(this.$api.user.getUserInfo).then(res => {
						this.userData = res.data;
						cache.put('setUserData', res.data, 24 * 60 * 60 * 90);
						//this.loadTabs();
					})

				}
				
			},
			loadTabs() {
				this.tabs = [];
				this.likes = [];
				this.$Request.get(this.$api.activity.getActClassify).then(
					res => {
						res.data.forEach(i => {
							if (i.cid != "1") {
								this.tabs.push(i)
							}
						})
						let cid = []
						cid = this.userData.interest.split(',');
						console.log('cid', cid)
						console.log('this.tabs', this.tabs)
						cid.forEach(i => {
							const tab = this.tabs.filter(v => v.cid == i);
							this.likes = [...this.likes, ...tab];
						})
					})
			},
			login() {
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						uni.login({
							success: (v) => {
								const params = {
									"code": v.code,
									"nickname": res.userInfo.nickName,
									"headImgUrl": res.userInfo.avatarUrl
								}
								this.$Request.post(this.$api.user.loginWithWechat, params).then(
									res => {
										if (res.code == 20000) {
											cache.put('token', res.data.token, 24 * 60 * 60 * 90)
											cache.put('setUserData', res.data.userinfo, 24 *
												60 * 60 * 90)
											uni.showToast({
												icon: 'success',
												position: 'bottom',
												title: '登录成功'
											});
											this.userData = res.data.userinfo;
											this.loadTabs();
										} else {
											uni.showToast({
												icon: 'error',
												position: 'bottom',
												title: res.data.message
											});
										}
										uni.hideLoading()
									})
							}
						})
					},
					fail: (res) => {
						console.log(res)
					}
				});
			},
			goEdit() {
				//const value = uni.getStorageSync('setUserData');
				const value = cache.get('setUserData', false);
				if (!value) {
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '用户未登陆！'
					});
				} else {
					uni.navigateTo({
						url: "/pages/my/components/setting"
					})
				}
			},
			goMenuPage(url) {
				uni.navigateTo({
					url: url
				})
			},
			adload() {
				console.log('')
				this.isLoad = true;
			},
			aderror(e) {
				this.adMessage = e.detail.errMsg;
			}
		}

	}
</script>

<style lang="scss">
	@import "index.scss";
</style>
